package com.suneee.cloud.group.dao.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.suneee.base.util.ContextHelper;
import com.suneee.cloud.group.dao.DePartMentDao;
import com.suneee.cloud.group.entity.DePartMentEntity;
import com.suneee.context.page.Page;
import com.suneee.core.error.Assert;
import com.suneee.core.util.StringUtils;
import com.suneee.orm.GenericDao;

@Repository("dePartMentDao")
public class DePartMentDaoImpl extends GenericDao<DePartMentEntity, Integer> implements DePartMentDao {

	@Override
	public Page<DePartMentEntity> queryDepartMentInfoPage(DePartMentEntity departEntity) {
/*		WhereClauseBuilder brand =  new WhereClauseBuilder();
		//设置分页信息
		departEntity.setPageNumber(departEntity.getPageNumber() == 0 ? 1 : departEntity.getPageNumber());
		departEntity.setPageSize(departEntity.getPageSize() == 0 ? 10 : departEntity.getPageSize());
		//设置排序
		departEntity.setSort((departEntity.getSort() == null || StringUtils.isEmpty(departEntity.getSort().getOrderByClause())) ? new Sort(
		                "update_time", Order.DESC) : departEntity.getSort());
		return this.selectWhere(brand.getSql(), departEntity, brand.getArgs());*/
		return null;
	}

	@Override
	public void insertDePartMentInfo(DePartMentEntity mentEntity) 
	{
		this.insert(mentEntity);
	}

	@Override
	public void updateDePartMentInfo(DePartMentEntity departEntity) 
	{
		String builder = "update t_department set  name = ? , update_time = ? where enterprise_code = ? and dept_code = ?";
		Object [] args = new Object[4];
		args[0] = departEntity.getName();
		args[1] = new Date();
		args[2] = departEntity.getEnterpriseCode();
		args[3] = departEntity.getDeptCode();
		this.executeUpdate(builder, args);
	}

	@Override
	public void deleteDePartMentInfo(DePartMentEntity departEntity) 
	{
		Object [] args = new Object[2];
		args[0] = departEntity.getEnterpriseCode();
		args[1] = departEntity.getDeptCode();
		this.deleteWhere("enterprise_code = ? and dept_code = ?", args);
	}

	@Override
	public List<DePartMentEntity> queryDePartMentList(int len,String deptCode) 
	{
		String entpriseCode = ContextHelper.getCurrentLoginInfo().getEnterpriseCode();
		if(StringUtils.isEmpty(deptCode))
		{
			Object [] args = new Object[2];
			args[0] = len;
			args[1] = entpriseCode;
			return this.selectWhere(" length(dept_code) = ? and enterprise_code = ? ", args);	
		}
		else
		{
			Object [] args = new Object[3];
			args[0] = len;
			args[1] = deptCode+"%";
			args[2] = entpriseCode;
			return this.selectWhere(" length(dept_code) = ? and dept_code like ?  and enterprise_code = ? ", args);	
		}
		
	}

	@Override
	public String maxDepartCode(String deptCode) 
	{
		List<Map<String, Object>> listMap = null;
		if(null == deptCode || "".equals(deptCode))
		{
			listMap = this.queryForList("select max(dept_code) as deptCode from t_department where length(dept_code) = ? ", 2);
		}
		else
		{
			listMap = this.queryForList("select max(dept_code) as deptCode from t_department where dept_code like ?", deptCode+"%");	
		}
		if(null != listMap && listMap.size() > 0)
		{
			if(null != listMap.get(0).get("deptCode") && !"".equals(listMap.get(0).get("deptCode")))
			{
				return listMap.get(0).get("deptCode").toString();	
			}
		}
		return null;
	}

	@Override
	public boolean isExitsInDPartName(DePartMentEntity departEntity) 
	{
		Assert.notNull(departEntity, "账号组对象不能为空");
		Assert.notNull(departEntity.getDeptCode(), "账号组编码不能为空");
		List<Map<String, Object>> listMap = null;
		String deptCode = "";
		String enterprise = ContextHelper.getCurrentLoginInfo().getEnterpriseCode();
		deptCode = departEntity.getDeptCode();
		//根据账号组编码判断该账号组属于几级账号组
		if(deptCode.length() >= 4)
		{
			Object[] args = new Object[4];
			args[0] = deptCode.substring(0,departEntity.getDeptCode().length()-2)+"%";
			args[1] = departEntity.getDeptCode().length();
			args[2] = departEntity.getName();
			args[3] = enterprise;
			listMap = this.queryForList(" SELECT * FROM t_department WHERE dept_code like ? AND LENGTH(dept_code) = ?  AND NAME = ? AND enterprise_code = ? ", args);
		}
		if(2 == deptCode.length())
		{
			Object[] args = new Object[3];
			args[0] = departEntity.getDeptCode().length();
			args[1] = departEntity.getName();
			args[2] = enterprise;
			listMap = this.queryForList(" SELECT * FROM t_department WHERE LENGTH(dept_code) = ? AND NAME = ? AND enterprise_code = ?", args);
		}
		return null != listMap && listMap.size() > 0;
	}
	
	public boolean isExitsUpDPartName(DePartMentEntity departEntity) 
	{
		Assert.notNull(departEntity, "账号组对象不能为空");
		Assert.notNull(departEntity.getDeptCode(), "账号组编码不能为空");
		List<Map<String, Object>> listMap = null;
		String deptCode = "";
		String enterprise = ContextHelper.getCurrentLoginInfo().getEnterpriseCode();
		deptCode = departEntity.getDeptCode();
		//根据账号组编码判断该账号组属于几级账号组
		if(deptCode.length() >= 4)
		{
			Object[] args = new Object[5];
			args[0] = deptCode.substring(0,departEntity.getDeptCode().length()-2)+"%";
			args[1] = departEntity.getDeptCode().length();
			args[2] = departEntity.getName();
			args[3] = enterprise;
			args[3] = deptCode;
			listMap = this.queryForList(" SELECT * FROM t_department WHERE dept_code like ? AND LENGTH(dept_code) = ?  AND NAME = ? AND enterprise_code = ? and dept_code != ? ", args);
		}
		if(2 == deptCode.length())
		{
			Object[] args = new Object[3];
			args[0] = departEntity.getDeptCode().length();
			args[1] = departEntity.getName();
			args[2] = enterprise;
			listMap = this.queryForList(" SELECT * FROM t_department WHERE LENGTH(dept_code) = ? AND NAME = ? AND enterprise_code = ? ", args);
		}
		return null != listMap && listMap.size() > 0;
	}

	@Override
	public boolean isExitsdPartFlag(DePartMentEntity departEntity) 
	{
		List<Map<String, Object>> dList = null;
		List<Map<String, Object>> aList = null;
		//获取账号组编码
		String deptCode = departEntity.getDeptCode();
		//获取企业编码
		String enterprise = ContextHelper.getCurrentLoginInfo().getEnterpriseCode();
		Object[] args = new Object[2];
		
		//判断是几级账号组 且若不是第三级账号组则需要查询该账号组下是否有子账号组
		// 新逻辑：不管是哪一级有子级就不能删
		//if(departEntity.getDeptCode().length() < 6){
			args[0] = deptCode + "%";
			args[1] = enterprise;
			dList = this.queryForList(" SELECT * FROM t_department WHERE dept_code like ? AND enterprise_code = ?  ", args);
		//}
		//设置查询条件
		args[0] = deptCode;
		
		//查询是否有账号引用该账号组
		aList = this.queryForList("SELECT * FROM t_administrator where dept_code = ? AND enterprise_code = ? ", args);
		
		//一下条件满足一个则返回true  否则返回false 
		if((null != dList && dList.size() > 1) || (null != aList && aList.size() > 0))
		{
			return true;
		}
		return false;
	}
	
}
